package cn.stronglink.assetmanage.business.check.model;

import android.os.Environment;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.Result;
import cn.stronglink.assetmanage.entity.Task;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.ImageUtils;
import cn.stronglink.netrequest.business.check.CheckService;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * 验收入库数据
 * Created by chencongcong on 2018/6/18.
 */

public class CheckModel {
    private CheckService checkService;
    private Gson gson = new Gson();
    private onCheckDataListener checkDataListener;
    private List<Check> localCheckList;
    private Map<String, Check> orderMap = new HashMap<>();

    public onCheckDataListener getCheckDataListener() {
        return checkDataListener;
    }

    public void setCheckDataListener(onCheckDataListener checkDataListener) {
        this.checkDataListener = checkDataListener;
    }

    public CheckModel() {
        checkService = new CheckService();
        getLocalCheckList();

    }

    /**
     * 获取本地数据库所有单子
     */
    private void getLocalCheckList() {
        localCheckList = DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getCheckDao().loadAll();
        if (localCheckList != null && localCheckList.size() > 0) {
            for (int i = 0; i < localCheckList.size(); i++) {
                Check check = localCheckList.get(i);
                orderMap.put(check.getId_(), check);
            }

        }
    }

    public void getCheckData() {
        try {
            checkService.downloadPresetWorkOrder(new Callback() {
                @Override
                public void onFailure(Call call, final IOException e) {
                    if (checkDataListener != null) {
                        checkDataListener.onFailer(call, e);
                    }
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (checkDataListener != null) {
                        checkDataListener.onResponse(call, response);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void saveCheckData(String requestJson) {
        Result<List<Check>> checkResult = null;
        checkResult = gson.fromJson(requestJson, new TypeToken<Result<List<Check>>>() {
        }.getType());
        if (checkResult != null && checkResult.getHttpCode() == HttpURLConnection.HTTP_OK) {
            if (checkResult.getData() != null) {
                List<Check> checkList = checkResult.getData();
                // 验收单列表
                if (checkList != null && checkList.size() > 0) {
                    //从服务器获取入库验收数据，成功后，保留本地已经有的单子
                    for (Check check : checkList) {
                        //本地没有的单子才会保存
                        if (!orderMap.containsKey(check.getId_())) {
                            check.setOrderType(Constants.STATUS_THREE);
                            DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getCheckDao().insert(check);
                            // 获取详情
                            if (check.getAssetList() != null && check.getAssetList().size() > 0) {
                                for (Asset asset : check.getAssetList()) {
                                    asset.setOrderNo(check.getId_());
                                    String base64Str = asset.getImagpath();
                                    if (base64Str != null) {
                                        String pictureUrl = ImageUtils.base64ToFile(base64Str);
                                        if (pictureUrl != null && !"".equals(pictureUrl)) {
                                            asset.setPictureUri(pictureUrl);
                                        }
                                    }
                                    DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getAssetDao().insert(asset);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public interface onCheckDataListener {
        void onFailer(Call call, IOException ex);

        void onResponse(Call call, Response response);
    }
}
